package com.kystar.muxplayer.manager;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;

import com.kystar.muxplayer.BaseApp;
import com.kystar.muxplayer.R;
import com.kystar.muxplayer.base.TLog;
import com.kystar.muxplayer.base.adapter.BaseQuickAdapter;
import com.kystar.muxplayer.base.adapter.DefaultItemTouchHelpCallback;
import com.kystar.muxplayer.base.adapter.OnItemChildClickListener;
import com.kystar.muxplayer.base.adapter.QuickAdapter;
import com.kystar.muxplayer.base.adapter.VHolder;
import com.kystar.muxplayer.model.Layer;
import com.kystar.muxplayer.selfview.PvmFrameLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Created by peanut on 2018/8/17
 */
public class WindowsManager {
    int[] enable = new int[]{
            R.drawable.pip_main_enable,
            R.drawable.pip1_enable,
            R.drawable.pip2_enable,
            R.drawable.pip3_enable,
    };
    int[] disable = new int[]{
            R.drawable.pip_main_disable,
            R.drawable.pip1_disable,
            R.drawable.pip2_disable,
            R.drawable.pip3_disable
    };

    Layer[] mLayers;
    Context mContext;
    RecyclerView mRecyclerView;
    PvmFrameLayout mPvmFrameLayout;
    List<Layer> mList = new ArrayList<>();
    QuickAdapter<Layer> mAdapter;
    PvmManager mPvmManager;
    static WindowsManager sWindowsManager;

    public static WindowsManager getInstance() {
        if (sWindowsManager == null) {
            sWindowsManager = new WindowsManager();
        }
        return sWindowsManager;
    }

    private WindowsManager() {

    }

    public void initView(RecyclerView recyclerView, PvmFrameLayout frameLayout) {
        mRecyclerView = recyclerView;
        mContext = recyclerView.getContext();
        mPvmFrameLayout = frameLayout;
        mPvmManager = PvmManager.getPvmSingleInstance();
        mLayers = mPvmManager.mLayers;
        init();
    }

    public void init() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new QuickAdapter<Layer>(R.layout.item_windows_condition) {
            @Override
            protected void convert(VHolder holder, Layer layer) {
                String name = layer.getLayerType().name;
                switch (name) {
                    case "MAIN":
                        if (layer.isSelected()) {
                            holder.setImageResource(R.id.pip_icon, enable[0]);
                        } else {
                            holder.setImageResource(R.id.pip_icon, disable[0]);
                        }
                        break;
                    case "PIP-1":
                        if (layer.isSelected()) {
                            holder.setImageResource(R.id.pip_icon, enable[1]);
                        } else {
                            holder.setImageResource(R.id.pip_icon, disable[1]);
                        }
                        break;
                    case "PIP-2":
                        if (layer.isSelected()) {
                            holder.setImageResource(R.id.pip_icon, enable[2]);
                        } else {
                            holder.setImageResource(R.id.pip_icon, disable[2]);
                        }
                        break;
                    case "PIP-3":
                        if (layer.isSelected()) {
                            holder.setImageResource(R.id.pip_icon, enable[3]);
                        } else {
                            holder.setImageResource(R.id.pip_icon, disable[3]);
                        }
                        break;
                }
                if (layer.isVisible()) {
                    holder.setImageResource(R.id.pip_on, R.drawable.pip_on);
                } else {
                    holder.setImageResource(R.id.pip_on, R.drawable.pip_off);
                }
                if (layer.isFullScreen()) {
                    holder.setImageResource(R.id.pip_fullscreen, R.drawable.pip_fullscreen_on);
                } else {
                    holder.setImageResource(R.id.pip_fullscreen, R.drawable.pip_fullscreen_off);
                }
                holder.addOnClickListener(R.id.pip_fullscreen);
                holder.addOnClickListener(R.id.pip_on);
                holder.addOnClickListener(R.id.pip_icon);
            }
        };
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Layer layer =  (Layer) adapter.getData().get(position);
                switch (view.getId()) {
                    case R.id.pip_icon:
                        for (int i = 0; i < adapter.getData().size(); i++) {
                            if (i == position) continue;
                            ((Layer) adapter.getData().get(i)).setSelected(false);
                        }
                        layer.setSelected(!layer.isSelected());
                        mPvmManager.getFrameLayout().setSelectLayer(layer);
                        break;
                    case R.id.pip_fullscreen:
                        layer.setFullScreen(!layer.isFullScreen());
                        mPvmFrameLayout.setChildFullScreenOrOldLocation(layer, layer.isFullScreen());
                        mPvmFrameLayout.post(() -> mPvmFrameLayout.setOutputSize(position, mPvmFrameLayout.getChildAt(position)));
                        break;
                    case R.id.pip_on:
                        layer.setVisible(!layer.isVisible());
                        mPvmFrameLayout.setChildVisible(layer, layer.isVisible());
                        break;
                }
                mAdapter.notifyDataSetChanged();
            }
        });

        ItemTouchHelper mItemTouchHelper = new ItemTouchHelper(new DefaultItemTouchHelpCallback((srcPosition, targetPosition) -> {
            Collections.swap(mAdapter.getData(), srcPosition, targetPosition);
            Layer srcLayer = mList.get(srcPosition);
            Layer tarLayer = mList.get(targetPosition);
            mAdapter.notifyItemMoved(srcPosition, targetPosition);
            mPvmFrameLayout.swap(srcPosition, targetPosition,srcLayer,tarLayer);
            TLog.e("test","srcPositon",srcPosition,"targetPositon",targetPosition);
            return true;
        }));
        mItemTouchHelper.attachToRecyclerView(mRecyclerView);
        Layer[] layers = BaseApp.getSettings().getPVWConfig().getLayers();
        setLayers(layers);
    }

    public void setLayers(Layer[] layers) {
        Layer[] sortLayer = new Layer[4];
        for (int i = 0; i < layers.length; i++) {
            int level = layers[i].getOrder();
            sortLayer[level-1] = layers[i];//
            TLog.e("test","level",level);
        }
        for (int i = 0; i < sortLayer.length; i++) {
            if(sortLayer[i] == null) throw new NullPointerException();
        }
        mList = Arrays.asList(sortLayer);
        mAdapter.setNewData(mList);
    }

}
